package zabbix

import (
	"encoding/json"
	"errors"
)

func IconmapCreateData() (string,map[string]interface{}) {
	method := "iconmap.create"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "name": "Type icons",
        "default_iconid": "2",
        "mappings": [
            {
                "inventory_link": 1,
                "expression": "server",
                "iconid": "3"
            },
            {
                "inventory_link": 1,
                "expression": "switch",
                "iconid": "4"
            }
        ]
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func IconmapDeleteData() (string,map[string]interface{}) {
	method := "iconmap.delete"
	var data map[string]interface{}
	jsonstr := []byte(`[
		"2",
		"5"
	]`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func IconmapUpdateData() (string,map[string]interface{}) {
	method := "iconmap.update"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "iconmapid": "1",
        "name": "OS icons"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func IconmapGetData() (string,map[string]interface{}) {
	method := "iconmap.get"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "iconmapids": "3",
        "output": "extend",
        "selectMappings": "extend"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}


func IconmapCreate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := IconmapCreateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func IconmapDelete(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := IconmapDeleteData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func IconmapUpdate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := IconmapUpdateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func IconmapGet(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := IconmapGetData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}



